package com.sgb.kjwl.view.ui.fragment.manage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentCompanyTrendsShopBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.manage.CompanyAssetsEntity;
import com.sgb.kjwl.model.entity.manage.IndustryHonoursEntity;
import com.sgb.kjwl.view.ui.adapter.manage.CompanyPropertyAllAdapter;
import com.sgb.kjwl.view.ui.adapter.manage.IndustryQualificationAdapter;
import com.sgb.kjwl.viewmodel.manage.CompanyPropertyAllModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

public class  CompanyPropertyAllFragment extends AppFragment {

    @Inject
    CompanyPropertyAllModel propertyAllModel;

    private FragmentCompanyTrendsShopBinding binding;
    private String companyNo;
    private String typeNo;
    private boolean isLoadMore;
    private int companyPageNumber;
    private CompanyPropertyAllAdapter companyTradesAdapter;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        super.setupFragmentComponent(appComponent);
        DaggerEnterpriseManageFragComponent.builder().appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }

    public static CompanyPropertyAllFragment newInstance(String companyNo,String typeNo) {
        CompanyPropertyAllFragment fragment = new CompanyPropertyAllFragment();
        Bundle bundle = new Bundle();
        bundle.putString("companyNo", companyNo);
        bundle.putString("type", typeNo);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void setDataLoadMore(boolean isRefresh) {
        if (isRefresh) {
            isLoadMore = false;
            companyPageNumber = 1;
        } else {
            isLoadMore = true;
            companyPageNumber++;
        }
        Map<String, Object> map4 = new HashMap<>();
        map4.put("companyNo", companyNo);
        map4.put("typeNo", typeNo);
        map4.put("page", companyPageNumber);
        map4.put("limit", 20);
        propertyAllModel.getCompanyAssetsType(map4);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_company_trends_shop, container, false);
        if (getArguments() != null) {
            companyNo = getArguments().getString("companyNo");
            typeNo = getArguments().getString("type");
        }

        binding.xrvShopCompanyTrends.setLayoutManager(new LinearLayoutManager(getContext()));
        companyTradesAdapter = new CompanyPropertyAllAdapter(getContext(), null);
        binding.xrvShopCompanyTrends.setAdapter(companyTradesAdapter);

        binding.xrvShopCompanyTrends.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                setDataLoadMore(true);
            }

            @Override
            public void onLoadMore() {
                setDataLoadMore(false);
            }
        });

        setDataLoadMore(true);
        return binding.getRoot();
    }

    public void getCompanyAssetsType(List<CompanyAssetsEntity> data) {
        boolean isGetData = true;
        binding.xrvShopCompanyTrends.refreshComplete();
        if (isLoadMore) {
            if (null != data && data.size() > 0) {
                binding.xrvShopCompanyTrends.loadMoreComplete();
                companyTradesAdapter.addDatas(data);
            } else {
                binding.xrvShopCompanyTrends.setNoMore(true);
            }
            binding.xrvShopCompanyTrends.setVisibility(View.VISIBLE);
        } else {
            binding.xrvShopCompanyTrends.refreshComplete();
            if (null != data && data.size() > 0) {
                binding.xrvShopCompanyTrends.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                companyTradesAdapter.setDatas(data);
                binding.xrvShopCompanyTrends.setLoadingMoreEnabled(true);
            } else {
                companyTradesAdapter.setDatas(null);
                binding.xrvShopCompanyTrends.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }
}